package edu.cmu.ibill.formbeans;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.mybeans.form.FormBean;

import edu.cmu.ibill.util.StringProcessor;

public class EditPersonalBillForm extends FormBean {
	private String id;
	private String date;
	private String category;
	private String amount;
	private String comment;
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getCategory() {
		return category;
	}
	public void setCategory(String category) {
		this.category = category;
	}
	public String getAmount() {
		return amount;
	}
	public void setAmount(String amount) {
		this.amount = amount;
	}
	public String getComment() {
		return comment;
	}
	public void setComment(String comment) {
		this.comment = StringProcessor.process(comment);
	}
	
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public List<String> getValidationErrors() {
		List<String> errors = new ArrayList<String>();

		if (date == null || date.length() == 0) {
			errors.add("date is empty");
		}else{
			SimpleDateFormat format = new SimpleDateFormat( "MM/dd/yyyy" );
			
			try {
				java.util.Date d = format.parse(date);
				Calendar currentDate = Calendar.getInstance();
				if(currentDate.getTime().before(d)){
					errors.add("invalid date for a future time");
				}
			} catch (ParseException e) {
				errors.add("invalid format for date");
			}
			
		}
		
		if (category == null || category.length() == 0) {
			errors.add("category is empty");
		}else{
			if(Integer.parseInt(category)==0){
				errors.add("category is empty");
			}
		}
		
		if (amount == null || amount.length() == 0) {
			errors.add("amount is empty");
		}else{
			try{
				Double.parseDouble(amount);
			}catch(NumberFormatException e){
				errors.add("invalid format for amount");
			}
		}
		
		return errors;
	}
}
